package com.example.administrator.sqliteopenhelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class StuOpenHelper extends SQLiteOpenHelper {
    public StuOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /*
    * onCreate()创建,onUpgrade()升级
    * 这两个抽象方法只执行一次
    *
    * */
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.e("StuOpenHelper", "onCreate");
        String sql = "create table student (stuno varchar(20), name varchar(20), age int)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(oldVersion == 1 && newVersion == 2){
            //SQLite中不可同时修改多个列，只能一行一行执行
            String phone = "alter table student add phone varchar(20)";
            String wechat = "alter table student add wechat varchar(20)";
            db.execSQL(phone);
            db.execSQL(wechat);
        }
    }
}
